package com.dwxt.cdms.service;

import com.dwxt.cdms.base.Page;
import com.dwxt.cdms.constant.EquipCategory;
import com.dwxt.cdms.constant.ReceiveDataType;
import com.dwxt.cdms.dao.poct.Poct4Mapper;
import com.dwxt.cdms.dao.poct.Poct8Mapper;
import com.dwxt.cdms.entity.poct.Poct4;
import com.dwxt.cdms.entity.poct.Poct4Example;
import com.dwxt.cdms.entity.poct.Poct8;
import com.dwxt.cdms.entity.poct.Poct8Example;
import com.dwxt.cdms.util.ReqUtils;
import com.dwxt.common.base.BaseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author yanlong
 * @version 1.0
 * @className PoctService
 * @date 2021/5/9 9:52
 */
@Service
@Slf4j
public class PoctService {

    @Autowired
    private Poct4Mapper poct4Mapper;

    @Autowired
    private Poct8Mapper poct8Mapper;

    @Autowired
    private HealthService healthService;

    @Autowired
    private StationService stationService;

    public BaseResult receiveData(HttpServletRequest request) {
        JSONObject para = ReqUtils.getParaJsonObject(request);
        log.info("para:{}", para.toString());
        String category = para.getString("category");
        int customerId = para.getInt("customerId");
        String imei = para.getString("imei");
        String type = para.getString("type");
        String value = para.getString("value");
        if (EquipCategory.POCT4.equals(category)) {
            Poct4 poct4 = new Poct4();
            poct4.setCheckTime(new Date());
            poct4.setCreateTime(new Date());
            poct4.setCustomerId(customerId);
            poct4.setImei(imei);
            poct4.setType(type);
            poct4.setValue(value);
            poct4Mapper.insertSelective(poct4);
            if (ReceiveDataType.needWarnList.contains(type)) {
                healthService.judgeIsNormalAndInsertRecord(poct4);
            }
        } else if (EquipCategory.POCT7.equals(category)) {
            Poct8 poct8 = new Poct8();
            poct8.setCheckTime(new Date());
            poct8.setCreateTime(new Date());
            poct8.setCustomerId(customerId);
            poct8.setImei(imei);
            poct8.setType(type);
            poct8.setValue(value);
            poct8Mapper.insertSelective(poct8);
            if (ReceiveDataType.needWarnList.contains(type)) {
                healthService.judgeIsNormalAndInsertRecord(poct8);
            }
        }
        return BaseResult.ok();
    }

    public BaseResult pageGroup(Page page, String name, String type, String category) {
        Integer stationId = stationService.getLoginUserStation();
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List res = new ArrayList();
        if (EquipCategory.POCT4.equals(category)) {
            List<Poct4> poct4List = poct4Mapper.pageGroup(name, type, stationId);
            res.addAll(poct4List);
        } else {
            List<Poct8> poct8List = poct8Mapper.pageGroup(name, type, stationId);
            res.addAll(poct8List);
        }
        return BaseResult.ok(new PageInfo<>(res));
    }

    public BaseResult page(Page page, Integer customerId, String type, String category, Integer partId) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List res = new ArrayList();
        if (EquipCategory.POCT4.equals(category)) {
            List<Poct4> poct4List = poct4Mapper.page(customerId, type, partId);
            res.addAll(poct4List);
        } else {
            List<Poct8> poct8List = poct8Mapper.page(customerId, type, partId);
            res.addAll(poct8List);
        }
        return BaseResult.ok(new PageInfo<>(res));
    }

    public Poct4 selectLastPoct4DataByType(Integer customerId, String type) {
        Poct4Example example = new Poct4Example();
        example.setOrderByClause("create_time desc");
        example.createCriteria().andCustomerIdEqualTo(customerId).andTypeEqualTo(type);
        List<Poct4> poct4List = poct4Mapper.selectByExample(example);
        return poct4List.size() > 0 ? poct4List.get(0) : null;
    }

    public Poct8 selectLastPoct8DataByType(Integer customerId, String type) {
        Poct8Example example = new Poct8Example();
        example.setOrderByClause("create_time desc");
        example.createCriteria().andCustomerIdEqualTo(customerId).andTypeEqualTo(type);
        List<Poct8> poct8List = poct8Mapper.selectByExample(example);
        return poct8List.size() > 0 ? poct8List.get(0) : null;
    }

    public BaseResult updatePart(Map map) {
        List ids = (List) map.get("ids");
        Integer partId = (Integer) map.get("partId");
        String category = (String) map.get("category");
        int i = 0;
        if (EquipCategory.POCT4.equals(category)) {
            i = poct4Mapper.updatePart(ids, partId);
        } else {
            i = poct8Mapper.updatePart(ids, partId);

        }
        return BaseResult.ok(i);

    }

    public BaseResult dataList(Integer customerId, String type, Integer partId, Integer scope) {

        List<Poct4> poct4List = poct4Mapper.dataList(customerId, type, partId, scope);
        if (poct4List != null && poct4List.size() > 0) {
            return BaseResult.ok(poct4List);
        }
        List<Poct8> poct8List = poct8Mapper.dataList(customerId, type, partId, scope);
        return BaseResult.ok(poct8List);
    }
}
